package com.xxzx.publicprevention.service.impl;

import com.xxzx.publicprevention.mapper.GeoZhyhdDao;
import com.xxzx.publicprevention.pojo.GeoZhyhd;
import com.xxzx.publicprevention.service.GeoZhyhdService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * (GeoZhyhd)表服务实现类
 *
 * @author makejava
 * @since 2020-11-13 10:49:37
 */
@Service("geoZhyhdService")
public class GeoZhyhdServiceImpl implements GeoZhyhdService {
    @Resource
    private GeoZhyhdDao geoZhyhdDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GeoZhyhd queryById(Integer id) {
        return this.geoZhyhdDao.queryById(id);
    }

    @Override
    public List<GeoZhyhd> gis_Contains(String coords) {
        return geoZhyhdDao.gis_Contains(coords);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<GeoZhyhd> queryAllByLimit(int offset, int limit) {
        return this.geoZhyhdDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<GeoZhyhd> queryAll(GeoZhyhd geoZhyhd) {
        return geoZhyhdDao.queryAll(geoZhyhd);
    }

    /**
     * 新增数据
     *
     * @param geoZhyhd 实例对象
     * @return 实例对象
     */
    @Override
    public GeoZhyhd insert(GeoZhyhd geoZhyhd) {
        this.geoZhyhdDao.insert(geoZhyhd);
        return geoZhyhd;
    }

    /**
     * 修改数据
     *
     * @param geoZhyhd 实例对象
     * @return 实例对象
     */
    @Override
    public GeoZhyhd update(GeoZhyhd geoZhyhd) {
        this.geoZhyhdDao.update(geoZhyhd);
        return this.queryById(geoZhyhd.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.geoZhyhdDao.deleteById(id) > 0;
    }

    @Override
    public List<GeoZhyhd> getZhyhdByKeywords(String keywords) {
        return this.geoZhyhdDao.getZhyhdByKeywords(keywords);
    }



    @Override
    public List<String> getYhdYear() {
        List<String> list = new ArrayList<>();
        GeoZhyhd geoZhyhd = new GeoZhyhd();
        List<GeoZhyhd> zhyhds = queryAll(geoZhyhd);

        if (list.size() == 0) {
            list.add(zhyhds.get(0).getNd());
        }

        for (GeoZhyhd c : zhyhds) {

            boolean flag = true;
            if (c.getNd().trim().equals("")) {
                flag = false;
            } else {
                for (String k : list) {
                    if (k.equals(c.getNd())) {
                        flag = false;
                    }
                }

            }
            if (flag) {
                list.add(c.getNd());
            }

        }


        return list;
    }

    @Override
    public List<GeoZhyhd> getZhyhdByNd(String nd) {

        List<GeoZhyhd> byNd = new ArrayList<>();
        if (nd.equals("1970")) {
            GeoZhyhd geoZhyhd = new GeoZhyhd();
            byNd = this.geoZhyhdDao.queryAll(geoZhyhd);
        } else {
            byNd = this.geoZhyhdDao.getZhyhdByNd(nd);
        }

        return byNd;

    }

    @Override
    public List<GeoZhyhd> getZhyhdByNdAndKeywords(String nd, String keywords) {
        return this.geoZhyhdDao.getZhyhdByNdAndKeywords(nd, keywords);
    }


    public static String numOfImport(HSSFCell cell) {
        String value = cell.toString();
        int i = cell.getCellType();
        if (i == 1) {
            return value;
        } else {
            String[] str = value.split("\\.");
            if (str.length > 1) {
                String str1 = str[1];
                int m = Integer.parseInt(str1);
                if (m == 0) {
                    return str[0];
                } else {
                    return value;

                }
            } else {
                return value;
            }
        }


    }
}
